package com.gameGroups.Controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gameGroups.Dto.GiveLikeDto;
import com.gameGroups.Service.GiveLikeNumberService;
import com.gameGroups.Utils.MyException;
import com.gameGroups.Utils.StringUtil;


@Controller
@RequestMapping("/giveLikeNumber")
public class GiveLikeNumberContorller {
	
	@Autowired
	private GiveLikeNumberService giveLikeNumberService;
	
	//查询作品点赞互动
	@RequestMapping(value="/queryGiveLikeNumberList.do",method=RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> queryGiveLikeNumberList(HttpServletRequest request,HttpServletResponse response) throws MyException{
		   Map<String,Object> model=new HashMap<String, Object>();
		  String uid = request.getParameter("uid");
		try {
		 List<GiveLikeDto> GiveLikeList = giveLikeNumberService.queryGiveLikeNumberList(uid);
		 if (CollectionUtils.isEmpty(GiveLikeList)) {
			  model.put("status", "5555");
			  model.put("errorMessage", "没有人给你点赞,菜鸟！");
			 }else{
			  model.put("status", "0000"); 
			  model.put("GiveLikeList", GiveLikeList);
			 }			
		} catch (Exception e) {
			model.put("status", "9999");
			model.put("errorMessage", "系统异常!");
			throw new MyException("点赞互动作品功能异常!"+":"+e);
		 }
		 return model; 
	}
	
	//点赞
	@RequestMapping(value="addGiveLike.do")
	@ResponseBody
	public Map<String,Object> addGiveLike(@RequestBody GiveLikeDto giveLikeDto,HttpServletRequest request,HttpServletResponse response) throws MyException{
		Map<String,Object> model=new HashMap<String, Object>();
		try {
			giveLikeNumberService.addGiveLike(giveLikeDto);
			int wid = giveLikeDto.getWid();
			giveLikeNumberService.addWorksGiveLike(wid);
		    model.put("status", "0000");		  
		} catch (Exception e) {
			model.put("status", "9999");
			model.put("errorMessage", "系统异常!");
			throw new MyException("点赞功能异常!"+":"+e);
		}
		return model;
	}
	
	//取消赞
	@RequestMapping(value="deleteGiveLike.do")
	@ResponseBody
	public Map<String,Object> deleteGiveLike(@RequestBody GiveLikeDto giveLikeDto, HttpServletRequest request,HttpServletResponse response) throws MyException{
		Map<String,Object> model=new HashMap<String, Object>();
		try {
			giveLikeNumberService.deleteGiveLike(giveLikeDto);
	        int wid = giveLikeDto.getWid();
			giveLikeNumberService.deleteWorksGiveLike(wid);
			model.put("status", "0000");		  		
		} catch (Exception e) {
			model.put("status", "9999");
			model.put("errorMessage", "系统异常!");
			throw new MyException("取消点赞功能异常!"+":"+e);
		}
		return model;
	}
}
